package com.haitonghop.modules.truyentranh;

import java.util.ArrayList;
import java.util.Hashtable;

import android.content.Context;
import android.util.Log;

import com.haitonghop.data.TamDiemDetailModuleData;
import com.haitonghop.utils.sqllite.datasource.TruyenTranhBookMarkDataSource;
import com.haitonghop.utils.sqllite.datasource.TruyenTranhBookmark;

public class TruyenTranhSession {
	static private TamDiemDetailModuleData currentComic = null;
	static private TruyenTranhChapterData currentChapter = null;
	static private int currentPage = -1;
	static private TruyenTranhBookMarkDataSource bookMarkDataSource;
	public static TamDiemDetailModuleData getCurrentComic() {
		return currentComic;
	}

	public static void setCurrentComic(TamDiemDetailModuleData currentComic) {
		TruyenTranhSession.currentComic = new TamDiemDetailModuleData();
		
		TruyenTranhSession.currentComic.title = currentComic.title;
		TruyenTranhSession.currentComic.avatar = currentComic.avatar;
		TruyenTranhSession.currentComic.href = currentComic.href;		
	}

	public static TruyenTranhChapterData getCurrentChapter() {
		return currentChapter;
	}

	public static void setCurrentChapter(TruyenTranhChapterData currentChapter) {
		TruyenTranhSession.currentChapter = new TruyenTranhChapterData();
		
		TruyenTranhSession.currentChapter.setReadUrl(currentChapter.getReadUrl());
		TruyenTranhSession.currentChapter.setName(currentChapter.getName());
	}

	public static int getCurrentPage() {
		return currentPage;
	}

	public static void setCurrentPage(int currentPage) {
		TruyenTranhSession.currentPage = currentPage;
	}
	
	public static Hashtable<String, String> getAllBookMark(Context context){
		if(bookMarkDataSource == null){
			bookMarkDataSource = new TruyenTranhBookMarkDataSource(context);
		}
		
		return bookMarkDataSource.getAllBookMarksIdByReadUrl();
	}
	
	public static ArrayList<TruyenTranhBookmark> getBookmark(Context context){
		if(bookMarkDataSource == null){
			bookMarkDataSource = new TruyenTranhBookMarkDataSource(context);
		}
		
		return bookMarkDataSource.getAllBookmarks();
	}
	
	public static void deleteBookmark(Context context, String readUrl){

		if(bookMarkDataSource == null){
			bookMarkDataSource = new TruyenTranhBookMarkDataSource(context);
		}
		
		bookMarkDataSource.deletedTruyenTranh(readUrl);
	}

	public static void doSaveCurrentSession(Context context) {
				
		TruyenTranhBookmark bookmark = new TruyenTranhBookmark();
		bookmark.setCurrentChapter(currentChapter);
		bookmark.setCurrentComic(currentComic);
		bookmark.setCurrentPage(currentPage);
		
		if(bookMarkDataSource == null){
			bookMarkDataSource = new TruyenTranhBookMarkDataSource(context);
		}
		
		bookMarkDataSource.insertTruyenTranh(bookmark);	
	}
}
